package com.tenglong.teacher.ui.dialog;


import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.EditText;

import com.cuotibao.zhuoxiao.basics.utils.ToastUtils;
import com.tenglong.teacher.R;

/**
 * A simple {@link Fragment} subclass.
 */
public class DebugDialogFragment extends DialogFragment implements View.OnClickListener {


    private EditText content;
    private String KEY = "225225";
    private DebugDialogFragmentListener listener;

    public DebugDialogFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        return inflater.inflate(R.layout.fragment_debug_dialog, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView(view);
        setListeners(view);
    }

    private void setListeners(View view) {
        view.findViewById(R.id.cancle).setOnClickListener(this);
        view.findViewById(R.id.confirm).setOnClickListener(this);
    }

    private void initView(View view) {
        content = view.findViewById(R.id.content);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.confirm:
                String text = content.getText().toString().trim();
                if (TextUtils.isEmpty(text)) {
                    ToastUtils.showCenterToast("请输入密钥");
                    return;
                }
                if (!text.equals(KEY)) {
                    ToastUtils.showCenterToast("密钥错误");
                } else {
                    if (listener != null) {
                        listener.onSkipDebugActivity();
                    }
                }
            case R.id.cancle:
                dismiss();
                break;
        }
    }

    public DebugDialogFragment setDebugDialogFragmentListener(DebugDialogFragmentListener listener) {
        this.listener = listener;
        return this;
    }

    public interface DebugDialogFragmentListener {
        void onSkipDebugActivity();
    }
}
